package com.jianyun.jyzs.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jianyun.jyzs.R;
import com.jianyun.jyzs.activity.AnnouncementDetialActivity;
import com.jianyun.jyzs.bean.AnnouncementList;
import com.jianyun.jyzs.dao.AnnouncementDao;
import com.jianyun.jyzs.utils.LoginCache;
import com.jianyun.jyzs.widget.PromptDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.iwgang.simplifyspan.SimplifySpanBuild;
import cn.iwgang.simplifyspan.unit.SpecialImageUnit;

/**
 * Created by mlli on 2017/11/20.
 */

public class AnnouncementListAdapter extends RecyclerView.Adapter {



    private Context context;
    private List<AnnouncementList.Announcement> list;
    private AnnouncementDao dao;
    private final String enterpriseCode;

    public AnnouncementListAdapter(AnnouncementDao dao, Context context, List<AnnouncementList.Announcement> list) {
        this.context = context;
        this.list = list;
        this.dao = dao;
        enterpriseCode = LoginCache.getInstance().getLoginCache().getEnterpriseCode();
    }

    public void setList(List<AnnouncementList.Announcement> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_announcement, parent, false);
        return new MHolder(view);
    }

    private int[] imageRes = {R.drawable.notice_1, R.drawable.notice_2, R.drawable.notice_3, R.drawable.notice_4, R.drawable.notice_5,
            R.drawable.notice_6, R.drawable.notice_7, R.drawable.notice_8, R.drawable.notice_9, R.drawable.notice_5};

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final MHolder mHolder = (MHolder) holder;
        final AnnouncementList.Announcement announcement = list.get(position);
        mHolder.name.setText(announcement.getCreateMan() + "");
        if (!TextUtils.isEmpty(announcement.getDt()))
            mHolder.time.setText(announcement.getDt().substring(0, 10));
        mHolder.message.setText(announcement.getMsgCount() + "");
        mHolder.agree.setText(announcement.getAgreeCount() + "");
//        mHolder.title.setText(announcement.getTitle()+"");
        mHolder.message.setText(announcement.getMsgCount() + "");
        mHolder.agree.setText(announcement.getAgreeCount() + "");
        mHolder.image.setBackgroundResource(imageRes[(position + 1) % 10]);
        int isAttm = announcement.getIsAttm();
        if (isAttm == 1) {
            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.ggfujian);
            SpecialImageUnit imageUnit = new SpecialImageUnit(context, bitmap);
            SpannableStringBuilder build = new SimplifySpanBuild(announcement.getTitle()).append(imageUnit).build();
            mHolder.title.setText(build);
        } else {
            mHolder.title.setText(announcement.getTitle() + "");
        }
        if (announcement.isread()) {
            mHolder.tvUnRead.setVisibility(View.INVISIBLE);
        } else {
            mHolder.tvUnRead.setVisibility(View.VISIBLE);
        }
        mHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                final PromptDialog dialog = PromptDialog.newInstance(context, "提示", "删除这条公告？");
                dialog.setPromptButtonClickedListener(new PromptDialog.OnPromptButtonClickedListener() {
                    @Override
                    public void onPositiveButtonClicked() {
                        dao.del(announcement.getNewsId(), enterpriseCode);
                        list.remove(holder.getAdapterPosition());
                        notifyItemRemoved(holder.getAdapterPosition());

                    }

                    @Override
                    public void onNegativeButtonClicked() {
                        dialog.dismiss();
                    }
                });
                dialog.show();
                return false;
            }
        });
        mHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 context.startActivity(new Intent(context,AnnouncementDetialActivity.class).putExtra("bundle",announcement));
            }
        });

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class MHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image)
        ImageView image;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.time)
        TextView time;
        @BindView(R.id.message)
        TextView message;
        @BindView(R.id.agree)
        TextView agree;
        @BindView(R.id.textView4)
        TextView textView4;
        @BindView(R.id.tv_unRead)
        TextView tvUnRead;
        @BindView(R.id.tv_attach)
        TextView tvAttach;

        public MHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
